#include<iostream>
using namespace std;
const int N = 1100;
#include<map>
struct pos
{
    int x, y;
}a[N], b[N];
int n, m;
int f[N][N];
map<char, pair<int, int>> dire{{'N', {0, 1}}, {'E', {1, 0}}, 
        {'S', {0, -1}}, {'W', {-1, 0}}};
int dist(int x, int y)
{
    return (a[x].x - b[y].x) * (a[x].x - b[y].x) + 
    (a[x].y - b[y].y) * (a[x].y - b[y].y);
}
int main()
{
    cin >> n >> m;
    cin >> a[0].x >> a[0].y;
    cin >> b[0].x >> b[0].y;
    for (int i = 1; i <= n; i++)
    {
        char ch; cin >> ch;
        a[i] = {a[i - 1].x + dire[ch].first, a[i - 1].y + dire[ch].second};
    }
    for (int i = 1; i <= m; i++)
    {
        char ch; cin >> ch;
        b[i] = {b[i - 1].x + dire[ch].first, b[i - 1].y + dire[ch].second};
    }
    // zdl:: 接下来就是动态规划的逻辑, 首先需要钱初始化
    for (int i = 1; i <= n; i++) f[i][0] = f[i - 1][0] + dist(i, 0);
    for (int i = 1; i <= m; i++) f[0][i] = f[0][i - 1] + dist(0, i);

    // 接下来开始动态规划
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            f[i][j] = min(f[i - 1][j - 1], min(f[i][j - 1], f[i - 1][j])) + dist(i, j);
        }
    }
    cout << f[n][m] << endl;
    return 0;
}